package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 李旭
 * @date 2021/10/12 11:41
 * @Description: 首页
 */
@Controller
public class IndexController {


    @Autowired
    private ProductFeignClient productFeignClient;

    //首页  http://www.gmall.com/
    @GetMapping("/")
    public String index(Model model) {
        List<Map> list = getData();
        model.addAttribute("list", list);
        return "index/index";//Springmvc 整合了Thymeleaf
    }

    private List<Map> getData() {
        List<Map> list = new ArrayList<>();
        List<BaseCategoryView> baseCategoryViewList = productFeignClient.getBaseCategoryViewList();
        //K:Long  category1Id  1  V: List<BaseCategoryView>  长度 60
        //K:Long  category1Id  2  V: List<BaseCategoryView>  长度 24~25
        Map<Long, List<BaseCategoryView>> listMapByCategory1Id = baseCategoryViewList.stream().collect(
                Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        int index = 0;
        for (Map.Entry<Long, List<BaseCategoryView>> longListEntryByCategory1Id :
                                                     listMapByCategory1Id.entrySet()) {
            List<BaseCategoryView> valueByCategory1Id = longListEntryByCategory1Id.getValue();
            Map mapByCategory1Id = new HashMap();
            mapByCategory1Id.put("index",++index);
            mapByCategory1Id.put("categoryId",longListEntryByCategory1Id.getKey());
            mapByCategory1Id.put("categoryName", valueByCategory1Id.get(0).getCategory1Name());
            ////////////////////////////////////////
            List<Map> listMap = new ArrayList<>();
            Map<Long, List<BaseCategoryView>> listMapByCategory2Id = valueByCategory1Id.stream().
                    collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            for (Map.Entry<Long, List<BaseCategoryView>> longListEntryByCategory2Id :
                                          listMapByCategory2Id.entrySet()) {
                List<BaseCategoryView> valueByCategory2Id = longListEntryByCategory2Id.getValue();
                Map mapByCategory2Id = new HashMap();
                mapByCategory2Id.put("categoryId",longListEntryByCategory2Id.getKey());
                mapByCategory2Id.put("categoryName", valueByCategory2Id
                        .get(0).getCategory2Name());
                ///////////////偷天换日//////////////////////////
                List<Map> collect = valueByCategory2Id.stream().map(baseCategoryView -> {
                    Map map = new HashMap();
                    map.put("categoryId", baseCategoryView.getCategory3Id());
                    map.put("categoryName", baseCategoryView.getCategory3Name());
                    return map;
                }).collect(Collectors.toList());
                ////////////////////////////////////////
                mapByCategory2Id.put("categoryChild", collect);
                listMap.add(mapByCategory2Id);
            }
            //////////////////////////////////////////
            mapByCategory1Id.put("categoryChild",listMap);
            list.add(mapByCategory1Id);
        }
        return list;
    }

    @Autowired
    private TemplateEngine templateEngine;
    //人为触发 生成静态化的页面
    @GetMapping("/createHtml")
    @ResponseBody
    public Result createHtml(){
        //1:静态化页面的过程

        //2:数据
        List<Map> list = getData();
        Context context = new Context();
        context.setVariable("list",list);
        //3:模板
        String templateName = "index/index";
        //4:输出流
        Writer out = null;
        try {
            //编码是否正确  写UTF-8  结果是乱码   编码过滤器
        /*    out = new OutputStreamWriter(new FileOutputStream("D:\\temp\\index.html"),
                    "UTF-8");*/
            out = new PrintWriter("D:\\temp\\index.html","UTF-8");
            //读 UTF-8
            templateEngine.process(templateName,context,out);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(out != null)
                    out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return Result.ok();
    }
}
